<template>
  <el-card class="main-card">
    <div class="title">{{ this.$route.name }}</div>
    <!-- 表格操作 -->
    <div class="operation-container">
      <!-- 条件筛选 -->
      <div style="margin-left:auto">
        <!-- 登录方式 -->
        <el-select
            clearable
            v-model="sex"
            placeholder="请选择性别"
            size="small"
            style="margin-right:1rem"
        >
          <el-option
              v-for="item in sexList"
              :key="item.type"
              :label="item.desc"
              :value="item.type"
          />
        </el-select>
        <el-input
            v-model="keywords"
            prefix-icon="el-icon-search"
            size="small"
            placeholder="请输入患者姓名"
            style="width:200px"
            @keyup.enter.native="searchPatients"
        />
        <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            style="margin-left:1rem"
            @click="searchPatients"
        >
          搜索
        </el-button>
      </div>
    </div>
    <!-- 表格展示 -->
    <el-table border :data="patientList" v-loading="loading">
      <!-- 表格列 -->
      <el-table-column
          prop="patientName"
          label="患者姓名"
          align="center"
          width="100"
          fixed
      />
      <el-table-column
          prop="idNumber"
          label="身份证号"
          align="center"
          width="180"
          fixed
      />
      <el-table-column
          prop="sex"
          label="性别"
          align="center"
          width="60"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex == 0">女</el-tag>
          <el-tag type="success" v-if="scope.row.sex == 1">男</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="age"
          label="年龄"
          align="center"
          width="60"
      />
      <el-table-column
          prop="cultureStandard"
          label="文化程度"
          align="center"
          width="80"
      />
      <el-table-column
          prop="nation"
          label="民族"
          align="center"
          width="80"
      />
      <el-table-column
          prop="nationality"
          label="国籍"
          align="center"
          width="80"
      />
      <el-table-column
          prop="marriage"
          label="婚姻状态"
          align="center"
          width="80"
      />
      <el-table-column
          prop="household"
          label="户别"
          align="center"
          width="80"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.household == 0">农业</el-tag>
          <el-tag v-if="scope.row.household == 1">非农</el-tag>
          <el-tag v-if="scope.row.household == 2">不详</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="residentType"
          label="常驻类型"
          align="center"
          width="80"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.residentType == 0">户籍</el-tag>
          <el-tag v-if="scope.row.residentType == 1">非户籍</el-tag>
          <el-tag v-if="scope.row.residentType == 2">不详</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="bloodType"
          label="血型"
          align="center"
          width="80"
      >
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.bloodType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="height"
          label="身高"
          align="center"
          width="90"
      >
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.height }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="weight"
          label="体重"
          align="center"
          width="80"
      >
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.weight }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="RHBloodType"
          label="RH血型阴性"
          align="center"
          width="120"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.residentType == 0">是</el-tag>
          <el-tag v-if="scope.row.residentType == 1">否</el-tag>
          <el-tag v-if="scope.row.residentType == 2">不详</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="waistLine"
          label="腰围"
          align="center"
          width="80"
      >
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.waistLine }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="hipLine"
          label="臀围"
          align="center"
          width="80"
      >
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.hipLine }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="exposureHistory"
          label="暴露史"
          align="center"
          width="70"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.exposureHistory == 0">无</el-tag>
          <el-tag type="danger" v-if="scope.row.exposureHistory == 1">有</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="drugAllergyHistory"
          label="药物过敏史"
          align="center"
          width="120"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.drugAllergyHistory == 0">无</el-tag>
          <el-tag type="danger" v-if="scope.row.drugAllergyHistory == 1">有</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="geneticHistory"
          label="遗传病史"
          align="center"
          width="80"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.geneticHistory == 0">无</el-tag>
          <el-tag type="danger" v-if="scope.row.geneticHistory == 1">有</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="disabilityHistory"
          label="残疾情况"
          align="center"
          width="80"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.disabilityHistory == 0">无</el-tag>
          <el-tag type="danger" v-if="scope.row.disabilityHistory == 1">有</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="smokingHistory"
          label="吸烟史"
          align="center"
          width="70"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.smokingHistory == 0">无</el-tag>
          <el-tag type="danger" v-if="scope.row.smokingHistory == 1">有</el-tag>
          <el-tag type="info" v-if="scope.row.smokingHistory == 2">已戒</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="drinkHistory"
          label="饮酒史"
          align="center"
          width="70"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.drinkHistory == 0">无</el-tag>
          <el-tag type="danger" v-if="scope.row.drinkHistory == 1">有</el-tag>
          <el-tag type="info" v-if="scope.row.drinkHistory == 2">已戒</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="physicalExercise"
          label="体育锻炼"
          align="center"
          width="80"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.physicalExercise == 0">无</el-tag>
          <el-tag type="danger" v-if="scope.row.physicalExercise == 1">有</el-tag>
          <el-tag type="info" v-if="scope.row.physicalExercise == 2">有</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="sleepTime"
          label="睡眠时间"
          align="center"
          width="110"
      >
        <template slot-scope="scope">
          <el-tag type="info">{{ scope.row.sleepTime }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="sleepCase"
          label="睡觉情况"
          align="center"
          width="100"
      >
        <template slot-scope="scope">
          <el-tag type="info">{{ scope.row.sleepCase }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间"
          width="130"
          align="center"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" style="margin-right:5px"/>
          {{ scope.row.createTime | date }}
        </template>
      </el-table-column>
      <!-- 列操作 -->
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button
              type="primary"
              size="mini"
              @click="openCaseList(scope.row)"
          >
            病例列表
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
        class="pagination-container"
        background
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="current"
        :page-size="size"
        :total="count"
        :page-sizes="[10, 20]"
        layout="total, sizes, prev, pager, next, jumper"
    />
    <!-- 对话框 -->
    <el-dialog :visible.sync="isEdit" width="30%">
      <div class="dialog-title-container" slot="title">
        病例列表
      </div>
      <el-table :data="caseList">
        <el-table-column
            prop="consultationsHospital"
            label="就诊医院"
            align="center"
            width="140"
        />
        <el-table-column
            prop="consultationsTime"
            label="就诊时间"
            width="190"
            align="center"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" style="margin-right:5px"/>
            {{ scope.row.consultationsTime | date }}
          </template>
        </el-table-column>
        <!-- 列操作 -->
        <el-table-column label="操作" align="center" width="120" fixed="right">
          <template slot-scope="scope">
            <el-button
                type="primary"
                size="mini"
                @click="openCase(scope.row)"
            >
              <i class="el-icon-view"/>查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer">
        <el-button @click="isEdit = false">取 消</el-button>
        <el-button type="primary" @click="editUserRole">
          确 定
        </el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="caseInfo" width="40%">
      <div class="dialog-title-container-app" slot="title" ref="caseDetail"/>
      <el-descriptions class="margin-top" :column="1" size="medium" border style="margin-top: 50px">
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            就诊人
          </template>
          {{ caseFrom.patientName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-time"></i>
            就诊时间
          </template>
          {{ caseFrom.consultationsTime  | date }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-office-building"></i>
            就诊医院
          </template>
          {{ caseFrom.consultationsHospital }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-school"></i>
            就诊科室
          </template>
          {{ caseFrom.consultationsDepartment }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-s-custom"></i>
            医生姓名
          </template>
          {{ caseFrom.doctorName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            病例类型
          </template>
          <el-tag size="small" v-if="caseFrom.caseType == 0">门诊病例</el-tag>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-picture"></i>
            病例图片
          </template>
          <el-image
              style="width: 250px; height: 300px"
              :src="caseFrom.caseImage"
              :preview-src-list="caseImage"
              fit="fill"></el-image>
        </el-descriptions-item>
        <el-descriptions-item >
          <template slot="label">
            <i class="el-icon-document"></i>
            病情概要
          </template>
          {{ caseFrom.diseaseSummary }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-time"></i>
            上传时间
          </template>
          {{ caseFrom.createTime | dateTime }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  created() {
    this.listPatients();
  },
  data: function () {
    return {
      caseInfo: false,
      loading: true,
      isEdit: false,
      sex: null,
      patientForm: {},
      caseFrom: {},
      caseList: [],
      sexList: [{
        type: 0,
        desc: "女"
      }, {
        type: 1,
        desc: "男"
      }],
      patientList: [],
      caseImage:[],
      keywords: null,
      current: 1,
      size: 10,
      count: 0
    };
  },
  methods: {
    searchPatients() {
      this.current = 1;
      this.listPatients();
    },
    sizeChange(size) {
      this.size = size;
      this.listPatients();
    },
    currentChange(current) {
      this.current = current;
      this.listPatients();
    },
    openCaseList(patient) {
      this.patientForm = JSON.parse(JSON.stringify(patient));
      this.axios
          .get("/api/admin/listCase/" + patient.id, {})
          .then(({data}) => {
            this.caseList = data.data;
            console.log("case", this.caseList)
          })
      this.isEdit = true;
    },
    openCase(caseFrom) {
      this.caseFrom = JSON.parse(JSON.stringify(caseFrom));
      //先将数组清空
      this.caseImage.length = 0;
      //设置病例图片
      this.caseImage.push(caseFrom.caseImage);
      this.$refs.caseDetail.innerHTML = "病例信息";
      this.caseInfo = true;
    },
    editUserRole() {
      this.axios
          .put("/api/admin/users/role", this.patientForm)
          .then(({data}) => {
            if (data.flag) {
              this.$notify.success({
                title: "成功",
                message: data.message
              });
              this.listUsers();
            } else {
              this.$notify.error({
                title: "失败",
                message: data.message
              });
            }
            this.isEdit = false;
          });
    },
    listPatients() {
      this.axios
          .get("/api/admin/listPatient", {
            params: {
              current: this.current,
              size: this.size,
              keywords: this.keywords,
              sex: this.sex
            }
          })
          .then(({data}) => {
            this.patientList = data.data.recordList;
            this.count = data.data.count;
            this.loading = false;
          });
    },
  },
};
</script>
